package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.UserPages;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.Page;
import com.ruoyi.service.IPageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 页面组件Controller
 * 
 * @author ruoyi
 * @date 2025-06-14
 */
@RestController
@RequestMapping("/home/page")
public class PageController extends BaseController
{
    @Autowired
    private IPageService pageService;

    /**
     * 查询页面组件列表
     */
   // @PreAuthorize("@ss.hasPermi('home:page:list')")
    @GetMapping("/list")
    public TableDataInfo list(Page page)
    {
        startPage();
        List<Page> list = pageService.selectPageList(page);
        return getDataTable(list);
    }

    ///拿到自己的页面
    @GetMapping("/getList")
    public AjaxResult getUserPageList()
    {
        List<Page> list = pageService.getUserPageList();
//        return getDataTable(list);
        return AjaxResult.success(list);
    }

    @GetMapping("/userPages/{userId}")
    public TableDataInfo userPages(Page page,@PathVariable("userId") Long userId)
    {
        startPage();
        List<UserPages> list = pageService.selectUserPages(page,userId);
        return getDataTable(list);
    }

    /**
     * 导出页面组件列表
     */
   // @PreAuthorize("@ss.hasPermi('home:page:export')")
    @Log(title = "页面组件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Page page)
    {
        List<Page> list = pageService.selectPageList(page);
        ExcelUtil<Page> util = new ExcelUtil<Page>(Page.class);
        util.exportExcel(response, list, "页面组件数据");
    }

    /**
     * 获取页面组件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('home:page:query')")
    @GetMapping(value = "/{pageId}")
    public AjaxResult getInfo(@PathVariable("pageId") Long pageId)
    {
        return success(pageService.selectPageByPageId(pageId));
    }

    /**
     * 新增页面组件
     */
 //   @PreAuthorize("@ss.hasPermi('home:page:add')")
    @Log(title = "页面组件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Page page)
    {
        return toAjax(pageService.insertPage(page));
    }

    /**
     * 修改页面组件
     */
   // @PreAuthorize("@ss.hasPermi('home:page:edit')")
    @Log(title = "页面组件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Page page)
    {
        return toAjax(pageService.updatePage(page));
    }

   // @Log(title = "页面组件", businessType = BusinessType.UPDATE)
    @GetMapping("/updateUserPage/{userId}/{pageId}/{status}")
    public AjaxResult updateUserPage(@PathVariable("userId")Long userId,@PathVariable("pageId")Long pageId,@PathVariable("status")Integer status)
    {
        pageService.updateUserPage(userId,pageId,status);
        return  AjaxResult.success();
    }

    /**
     * 删除页面组件
     */
  //  @PreAuthorize("@ss.hasPermi('home:page:remove')")
    @Log(title = "页面组件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pageIds}")
    public AjaxResult remove(@PathVariable Long[] pageIds)
    {
        return toAjax(pageService.deletePageByPageIds(pageIds));
    }
}
